package com.gexin.controller.system.agent;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.ExportDocBeetl;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.agent.*;
import com.gexin.model.order.Order;
import com.gexin.model.system.AdminUser;
import com.gexin.model.weixin.WeixinUser;
import com.gexin.service.agent.AgentService;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static com.gexin.model.agent.Agent.*;

@ControllerBind(controllerKey="/system/agent", viewPath="WEB-INF/system/agent/")
public class AgentController
		extends SystemBaseController
{
	public void index()
	{
		if (isAjax())
		{
			String name = getPara("name");
			String phone = getPara("phone");
			String type = getPara("type");
			Page<Agent> agentPage = AgentService.dao.findAgentList(getpage(), getpageSize(), name, phone, type);

//			String productId = "2633";
//			AgentProductSku agentProductSku = new AgentProductSku();
//			List<Agent> list = agentPage.getList();
// 			for(int i=0;i<list.size();i++){
//				Agent agent1 = list.get(i);
//				agentProductSku.setId(UUID.randomUUID().toString());
//				agentProductSku.setAgentId(agent1.getId().toString());
//				agentProductSku.setProductId(productId);
//				agentProductSku.setSaleNum(0);
//				agentProductSku.setStockNum(0);
//				agentProductSku.save();
//			}

			renderJson(agentPage);
			return;
		}
		setAttr("levelList", AgentLevel.dao.findAll());
		render("agent_list.html");
	}

//	管理库存数量
	public void accountStockNum(){
			Integer agentId = getParaToInt("agent_id");
			if (isAjax())
			{
				if (agentId == null)
				{
					fail("代理商id不能为空!");
					return;
				}
				Page<AgentProductSku> agentProductSkuList = AgentProductSku.dao.findAgentProductSkubyId(getpage() , getpageSize(),agentId.toString());
				renderJson(agentProductSkuList);
				return;
			}
			setAttr("agentId", agentId);
			render("stock_num.html");
 	}

	public void updateStockNumForm(){
		Integer stockNum = getParaToInt("stockNum");
		if(null==stockNum||stockNum<0){
			fail("请输入库存数量并且库存数量大于0");
			return;
		}
		String id = getPara("id");
 		AgentProductSku agentProductSku = AgentProductSku.dao.findById(id);
		if(null==agentProductSku){
			agentProductSku = new AgentProductSku();
			agentProductSku.setStockNum(stockNum);
			agentProductSku.save();
		}else{
			agentProductSku.setStockNum(stockNum);
			agentProductSku.update();
		}
		success("修改成功");
	}

	public void auditList()
	{
		if (isAjax())
		{
			String name = getPara("name");
			String phone = getPara("phone");
			String type = getPara("type");
			Page<AgentCity> agentPage = AgentService.dao.findAgentAuditList(getpage(), getpageSize(), name, phone, type);
			renderJson(agentPage);
			return;
		}
		setAttr("levelList", AgentLevel.dao.findAll());
		render("audit_agent.html");
	}

	public void findAgentCity()
	{
		String id = getPara("id");
		List<AgentCity> agentCities = AgentCity.dao.findCityByAgentId(id);
		renderJson(agentCities);
	}

	public void audit()
	{
		Integer agentType = getParaToInt("agentType");
		AgentCity agentCity = (AgentCity)getModel(AgentCity.class, "agentCity");
		String agentId = ""+agentCity.getAgentId();
		Integer levelId = getParaToInt("level_id");
		Integer status = getParaToInt("status");


		if (Agent.TYPE_AREA == agentType)
		{
			String price = getPara("price");
			if (agentCity == null)
			{
				fail("������������null!");
				return;
			}
			String deposit = getPara("deposit");
			String sales = getPara("sales");
			String agencyFee = getPara("agency_fee");
			ApiResponse apiResponse = AgentService.dao.audit(agentCity, ""+levelId, price, getLogintUser(), deposit, sales, agencyFee);
			renderJson(apiResponse);
		}
		else if (Agent.TYPE_NORM == agentType)
		{
 			ApiResponse apiResponse = AgentService.dao.auditNormAgent(agentId, levelId, status, getLogintUser());
			renderJson(apiResponse);
		}
		else if (Agent.TYPE_ER == agentType)
		{
 			String discountStr = getPara();
			ApiResponse apiResponse = AgentService.dao.auditErJiAgent(agentId, levelId, status, getLogintUser(), discountStr);
			renderJson(apiResponse);
		}
		else if (Agent.TYPE_TEYUE== agentType)
		{
//  			String discountStr = getPara("discountStr");
			AgentService.dao.auditTeYueAgent(agentId, status, getLogintUser());
 			renderJson("apiResponse");
		}
	}

	public void orderList()
	{
		Integer agentId = getParaToInt("agent_id");
		if (isAjax())
		{
			if (agentId == null)
			{
				fail("������id������null!");
				return;
			}
			Page<Order> orderListByAgentId = AgentService.dao.findOrderListByAgentId(agentId, Integer.valueOf(getpage()), Integer.valueOf(getpageSize()));
			renderJson(orderListByAgentId);
			return;
		}
		setAttr("agentId", agentId);
		render("orderList.html");
	}

	public void accountList()
	{
		Integer agentId = getParaToInt("agent_id");
		if (isAjax())
		{
			if (agentId == null)
			{
				fail("������id������null!");
				return;
			}
			Page<AgentAccountLog> accountListByAgentId = AgentService.dao.findAccountListByAgentId(agentId, getpage(), getpageSize());
			renderJson(accountListByAgentId);
			return;
		}
		setAttr("agentId", agentId);
		render("agent_account.html");
	}

	public void recharge()
	{
		String price = getPara("price");
		Integer id = getParaToInt("id");
		ApiResponse recharge = AgentService.dao.recharge(price, id);
		renderJson(recharge);
	}

	public void withdrawalHtml()
	{
		if (isAjax())
		{
			Page<AgentAccountLog> page = AgentAccountLog.dao.findWithdrawal(getpage(), getpageSize());
			renderJson(page);
			return;
		}
		render("agent_withdrawal.html");
	}

	public void withdrawal()
	{
		String agentAccountLogId = getPara("id");
		Boolean state = getParaToBoolean("state");
		AdminUser adminUser = getLogintUser();
		String bankName = getPara("bank_name");
		String bankNum = getPara("bank_num");
		String bankUserName = getPara("bank_user_name");
		ApiResponse apiResponse = AgentService.dao.withdrawalOption(agentAccountLogId, state, adminUser, bankName, bankNum, bankUserName);
		renderJson(apiResponse);
	}

	public void extensionUser()
	{
		String agentId = getPara("agent_id");
		if (isAjax())
		{
			if (StringUtil.isBlank(agentId))
			{
				fail("������id������null!");
				return;
			}
			Page<WeixinUser> page = WeixinUser.dao.findByExtensionId(getpage(), getpageSize(), agentId);
			renderJson(page);
			return;
		}
		setAttr("agentId", agentId);
		render("extensionUser.html");
	}

	public void extensionAgent()
	{
		String agentId = getPara("agent_id");
		if (isAjax())
		{
			if (StringUtil.isBlank(agentId))
			{
				fail("������id������null!");
				return;
			}
			Page<Agent> page = Agent.dao.findByExtensionAgentId(getpage(), getpageSize(), agentId);
			renderJson(page);
			return;
		}
		setAttr("agentId", agentId);
		render("extensionAgent.html");
	}

	public void auditdelete()
	{
		String id = getPara();
		if (StringUtil.isBlank(id))
		{
			fail("id��������!");
			return;
		}
		Agent agent = (Agent)Agent.dao.findById(id);
		if (agent == null)
		{
			fail("������������!");
			return;
		}
		if (!Agent.STATUS_WEICHULI.equals(agent.getStatus()))
		{
			fail("����������������,����������!");
			return;
		}
		if ((Agent.CASH_DEPOSIT_YIZHIFU == agent.getCashDepositState()) || (Agent.PAY_TYPE_YIZHIFU == agent.getControlMoenyState()))
		{
			fail("����������������������������,��������!");
			return;
		}
		boolean delete = Agent.dao.deleteById(id);
		renderJson(new ApiResponse(delete, delete ? "��������!" : "��������!"));
	}

	public void changeAgentType()
	{
		String agentId = getPara("agent_id");
		if (StringUtil.isBlank(agentId)) {
			fail("������ID��������!");
		}
		Agent agent = (Agent)Agent.dao.findById(agentId);
 		boolean update;
		if ((agent.getIsPlatformAgent() == null) || (agent.getIsPlatformAgent().intValue() == 0))
		{
			agent.setIsPlatformAgent(Agent.ISPALTFROMTRUE);
			update = agent.update();
		}
		else
		{
			agent.setIsPlatformAgent(Agent.ISPALTFROMFALSE);
			update = agent.update();
		}
		renderJson(new ApiResponse(update, update ? "��������!" : "��������!"));
	}






}
